
<!-- 
    付款弹框
    企业信息-买入交易订单-待付款
    我是买方-我的竞价-付款
    我是买方-买方订单-付款 
-->

<template>
    <div>
        <!-- 付款弹框 -->
        <DialogInput ref="payment" title="买方确认付款操作">
            <div class="rem-dialog-con text_left">
                <FormTitle title="付款信息"/>
                <el-form label-width="120px" size="mini">
                    <el-form-item label="订单编号:">{{model.order_number}}</el-form-item>
                    <el-form-item label="平台服务费:" class="hpx-red-tips">{{model.commission}}个汇票豆</el-form-item>
                    <el-form-item label="贴现金额:" class="hpx-red-tips">{{model.order_total_price}}元</el-form-item>
                    <el-form-item label="付款户:">{{model.receiver_enterprise_name}}</el-form-item>
                    <el-form-item label="执剑人账户号:">{{model.receiver_account_number}}</el-form-item>
                </el-form>
                <el-row class="text_center">卖方已确认交易</el-row>
                <el-row class="text_center remind hpx-red-tips">请支付票款至兴业数金代管专户</el-row>
                <el-row class="text_center hpx-red-tips"><i class="iconfont icon-daojishi1"></i>
                    已等待{{waiteH}}{{waiteHText}}{{this.waiteTimeForm.waitdateM}}分钟{{this.waiteTimeForm.waitdateS}}秒
                </el-row>
            </div>
            <span slot="Remfooter">
                <el-row class="sign_con_btn">
                    <el-checkbox v-model="checkedContract">我已阅读并同意<el-button type="text" @click="openBillContract()">《票据应收款转让服务合同》</el-button></el-checkbox>
                </el-row>
                <el-button @click="closeDialog()" type="info" size="small" class="hpx-cancel-color dialog-button">再考虑一下</el-button>
                <el-button @click="confirmPayment()" :disabled="!checkedContract" type="primary" size="small" class="dialog-button">付款</el-button>
            </span>
        </DialogInput>
        <!-- 合同 -->
        <BillContract ref="bill-contaract-dailog"/>
        <!-- 付款成功弹框 -->
        <DialogInput ref="payment-success" title="付款">
            <div class="rem-dialog-con reserva-dialog">
                <el-row class="hpx-success">
                    <i class="iconfont icon-chenggong"></i>
                </el-row>
                <el-row>
                    付款成功！
                </el-row>
            </div>
            <span slot="Remfooter">
                <el-button type="primary" size="small" class="dialog-button" @click="closeDialog()">确定</el-button>
            </span>
        </DialogInput>
    </div>
</template>

<script lang="ts">
    import {Vue, Component, Prop} from 'vue-property-decorator';
    import DialogInput from '@component/common/DialogInput.vue';
    import BillContract from '@component/common/BillContract.vue';
    import FormTitle from '@component/common/FormTitle.vue';
    import { setTimeout } from 'timers';
    import { mapState, mapGetters, mapMutations, mapActions } from 'vuex';
    @Component({
        components:{
            DialogInput,
            BillContract,
            FormTitle
        },
        computed: {
            ...mapState([
                'userInfo'
            ]),
            ...mapState('myBill', ['billContractCona'])
        },
        methods: {
            ...mapMutations('myBill', [
                'setBillContractCona'
            ]),
            ...mapActions([
                'getAllTips'
            ])
        }
    })
    export default class DialogPayment extends Vue {
        timer:any = null
        //单个订单信息
        singleOrderId:any = {}
        //订单详情
        model:any = {}
        //是否勾选合同
        checkedContract = true
        //等待时间
        waiteTimeForm: any = {
            difference: '',
            waitTimeD: '',
            waitTimeH: '',
            waitdateM: '',
            waitdateS: '',
            waitdate: ''
        }
    //methods
        //获取订单详情
        async getDetailData () {
            let res = await this.$http.get(`orders/order/${this.singleOrderId}`)
            if(res.status == 200){
                this.model = res.data
                let data = res.data;
                let billContractCona = {
                    drawer_name: data.drawer_name,
                    bill_number: data.bill_number,
                    acceptor_name: data.acceptor_name,
                    bill_sum_price: data.bill_sum_price,
                    bill_deadline_time: data.bill_deadline_time,
                    receiver_name: data.receiver_name,
                    receiver_legalname: '',
                    drawer_account_number: data.drawer_account_number,
                    drawer_account_person: data.drawer_account_person,
                    drawer_bank_name: data.drawer_account_bank,
                    drawer_bank_number: data.drawer_account_bank_number,
                    receiver_account_number: data.receiver_account_number,
                    receiver_account_person: data.receiver_account_person,
                    receiver_bank_name: data.receiver_account_bank,
                    receiver_bank_number: data.receiver_account_bank_number,
                    deal_price: data.real_deal_price,
                    total_deal_price: data.order_total_price,
                    server_price: '',
                    systemTime: ''
                }
                this.setBillContractCona(billContractCona);
                this.getSystemTime();
                this.getComission();
                this.getLegalInfo(this.model.drawer_id,1)
                this.getLegalInfo(this.model.receiver_id, 2);
            }
        }
        openDialog (id) {
            this.singleOrderId = id
            this.getDetailData()
        }
        closeDialog () {
            this.$refs['payment'].closeDialog();
            this.$refs['payment-success'].closeDialog();
        }
        // 根据企业id查询法人代表
        async getLegalInfo (enterprise_id: string|number, check: string|number) {
            let res = await this.$http.get(`enterpriseXingyeUser/getLegalInfo?enterpriseId=${enterprise_id}`)
            if(res.status == 200){
                if(check == 1){
                    // 卖方法人
                    this.billContractCona.drawer_legalname = res.data.legalName;
                }else if(check == 2){
                    // 买方法人
                    this.billContractCona.receiver_legalname = res.data.legalName;
                }
            }
        }
        //服务费
        async getComission () {
            let res = await this.$http.get(`tools/calculator/comission?acceptorTypeId=${this.model.acceptor_type_id}&sumPrice=${this.model.bill_sum_price}`)
            if(res.status == 200){
                this.billContractCona.server_price = res.data;
                this.$refs['payment'].openDialog();
            }
        }
        //系统时间
        async getSystemTime () {
            let res = await this.$http.get('tools/systemTime')
            if(res.status == 200){
                this.billContractCona.systemTime = res.data;
                let newSystemDate = new Date().getTime();
                this.waiteTimeForm.difference = newSystemDate - res.data;
            }
        }

        //等待时间
        async getWaitTime () {
            let newdate = new Date().getTime();
            if (this.waiteTimeForm.difference >= 0) {
                if(this.model.order_status_id == 804){
                    this.waiteTimeForm.waitdate = newdate - this.model.order_time - this.waiteTimeForm.difference;
                }else {
                    this.waiteTimeForm.waitdate = newdate - this.model.order_update_time - this.waiteTimeForm.difference;
                }
            } else {
                if(this.model.order_status_id == 804){
                    this.waiteTimeForm.waitdate = newdate - this.model.order_time + this.waiteTimeForm.difference;
                }else {
                    this.waiteTimeForm.waitdate = newdate - this.model.order_update_time + this.waiteTimeForm.difference;
                }
            }
            if(this.waiteTimeForm.waitdate > 1000){
                let waitTime = new Date(this.waiteTimeForm.waitdate);
                this.waiteTimeForm.waitTimeD = waitTime.getDate();
                if (this.waiteTimeForm.waitTimeD > 2) {
                    this.waiteTimeForm.waitTimeH = waitTime.getHours() - 8 + (this.waiteTimeForm.waitTimeD - 1) * 24;
                } else if (this.waiteTimeForm.waitTimeD > 1) {
                    this.waiteTimeForm.waitTimeH = waitTime.getHours() - 8 + 24;
                } else {
                    this.waiteTimeForm.waitTimeH = waitTime.getHours() - 8;
                }
                this.waiteTimeForm.waitdateM = waitTime.getMinutes();
                this.waiteTimeForm.waitdateS = waitTime.getSeconds();
            }else {
                this.waiteTimeForm.waitTimeH = 0;
                this.waiteTimeForm.waitdateM = 0;
                this.waiteTimeForm.waitdateS = 0;
            }
        }
        get waiteH () {
            return this.waiteTimeForm.waitTimeH == '' ? '' : this.waiteTimeForm.waitTimeH
        }
        get waiteHText () {
            return this.waiteTimeForm.waitTimeH == '' ? '' : '小时'
        }
        openBillContract () {
            this.$refs['bill-contaract-dailog'].openDialog()
        }
        //确认付款
        async confirmPayment () {
            let res:any = await this.$http.post(`/paying/xingyeapi/orderPay/${this.model.id}`)
            if(res.status == 200){
                this.$refs['payment'].closeDialog()
                this.$refs['payment-success'].openDialog()
                this.getAllTips();
                this.$emit('updataTable')
            }
        }
        
        created () {
            this.timer = setInterval(()=>{
                this.getWaitTime();
            },1000)
        }
        destroyed() {
            clearInterval(this.timer)
        }
    }
</script>

<style lang="scss" scoped>
    // .reserva-dialog{
    //     text-align: center;
    //     .hpx-success{
    //         margin-bottom: 12px !important;
    //         font-size: 30px !important;
    //     }
    // }
</style>